{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Java\n",
"\n",
"Processing is really Java, but allowing some short-cuts, and some special functions defined for you.\n",
"\n",
"Consider the very simple Processing program:\n",
"\n",
"```java\n",
"int x = 1;\n",
"println(x);\n",
"```\n",
"\n",
"Normally, you couldn't run this straight away in Java, because every line of code in Java must be in a class. So, in order to run that same code in Java, you'd need to turn that into:\n",
"\n",
"```java\n",
"public class MyClass {\n",
" public static void main(String[] args) {\n",
" int x = 1;\n",
" System.out.println(x);\n",
" }\n",
"}\n",
"```\n",
"Note that MyClass could be called anything in this example. The method `main` is a special method that will be called automatically. You can't just use println, but we must refer to its full name.\n",
"\n",
"We need to use `public` to indicate that it is accessible from outside the object. We use `static` to indicate that it doesn't need to be part of a instance.\n",
"\n",
"We'll explore three examples converting Processing code to regular Java below.\n",
"\n",
"\n",
"# Tutor Magic\n",
"\n",
"In Jupyter, there are some metacommands that don't belong to Processing, but are instead part of Jupyter. These are called \"magics\" and start with a percent character.\n",
"\n",
"A new magic is called `%%tutor` and will allow you to visualize Java code.\n",
"\n",
"This is useful for watching the sorting programs from last week.\n",
"\n",
"You may need to download/upload these to your own account to see the output."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%tutor\n",
"\n",
"public class SquaredSort {\n",
" public static void swap(int[] array, int i, int j) {\n",
" int tmp = array[i];\n",
" array[i] = array[j];\n",
" array[j] = tmp;\n",
" }\n",
"\n",
" public static void squared_sort() {\n",
" int comparisons = 0;\n",
" int swaps = 0;\n",
" int[] array = new int []{8, 3, 7, 2};\n",
" for (int i = 0; i < array.length - 1; i++) {\n",
" for (int j = i; j < array.length; j++) {\n",
" comparisons++;\n",
" if (array[i] > array[j]) {\n",
" swaps++;\n",
" swap(array, i, j);\n",
" }\n",
" }\n",
" }\n",
" }\n",
" \n",
" public static void main(String[] args) {\n",
" squared_sort();\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%tutor\n",
"\n",
"public class BubbleSort {\n",
" public static void swap(int[] array, int i, int j) {\n",
" int tmp = array[i];\n",
" array[i] = array[j];\n",
" array[j] = tmp;\n",
" }\n",
"\n",
" public static void bubble_sort() {\n",
" int comparisons = 0;\n",
" int swaps = 0;\n",
" int[] array = new int []{8, 3, 7, 2};\n",
"\n",
" int stop = array.length;\n",
" int last_swap;\n",
" while (stop != 0) {\n",
" last_swap = 0;\n",
" for (int i = 0; i < stop - 1; i++) {\n",
" comparisons++;\n",
" if (array[i] > array[i + 1]) {\n",
" swaps++;\n",
" swap(array, i, i + 1);\n",
" last_swap = i + 1;\n",
" }\n",
" }\n",
" stop = last_swap;\n",
" }\n",
" }\n",
" \n",
" public static void main(String[] args) {\n",
" bubble_sort();\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%tutor\n",
"\n",
"public class BubbleSort {\n",
" public static void swap(int[] array, int i, int j) {\n",
" int tmp = array[i];\n",
" array[i] = array[j];\n",
" array[j] = tmp;\n",
" }\n",
"\n",
" public static void bubble_sort() {\n",
" int comparisons = 0;\n",
" int swaps = 0;\n",
" int[] array = new int []{2, 3, 7, 8};\n",
"\n",
" int stop = array.length;\n",
" int last_swap;\n",
" while (stop != 0) {\n",
" last_swap = 0;\n",
" for (int i = 0; i < stop - 1; i++) {\n",
" comparisons++;\n",
" if (array[i] > array[i + 1]) {\n",
" swaps++;\n",
" swap(array, i, i + 1);\n",
" last_swap = i + 1;\n",
" }\n",
" }\n",
" stop = last_swap;\n",
" }\n",
" }\n",
" \n",
" public static void main(String[] args) {\n",
" bubble_sort();\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%tutor\n",
" \n",
"public class QuickSort {\n",
" public static int comparisons = 0;\n",
" public static int swaps = 0;\n",
"\n",
" public static void swap(int[] array, int i, int j) {\n",
" swaps++;\n",
" int tmp = array[i];\n",
" array[i] = array[j];\n",
" array[j] = tmp;\n",
" }\n",
"\n",
" public static void quicksort(int[] array, int lo, int hi) {\n",
" if (lo < hi) {\n",
" int p = partition(array, lo, hi);\n",
" quicksort(array, lo, p - 1);\n",
" quicksort(array, p + 1, hi);\n",
" }\n",
" }\n",
"\n",
" public static int partition(int[] array, int lo, int hi) {\n",
" int pivotIndex = choosePivot(lo, hi);\n",
" int pivotValue = array[pivotIndex];\n",
" // put the chosen pivot at array[hi]\n",
" swap(array, pivotIndex, hi);\n",
" int storeIndex = lo;\n",
" // Compare remaining array elements against pivotValue = array[hi]\n",
" for (int i = lo; i <= hi - 1; i++) {\n",
" comparisons++;\n",
" if (array[i] < pivotValue) {\n",
" swap(array, i, storeIndex);\n",
" storeIndex = storeIndex + 1;\n",
" }\n",
" }\n",
" swap(array, storeIndex, hi); // Move pivot to its final place\n",
" return storeIndex;\n",
" }\n",
"\n",
" public static int choosePivot(int lo, int hi) {\n",
" return ((hi + lo) / 2);\n",
" }\n",
" \n",
" public static void main(String[] args) {\n",
" int[] array = new int []{8, 3, 7, 2};\n",
" quicksort(array, 0, 3);\n",
" }\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Calysto Processing",
"language": "processing",
"name": "calysto_processing"
},
"language_info": {
"codemirror_mode": {
"name": "text/x-java",
"version": 2
},
"file_extension": ".java",
"mimetype": "text/x-java",
"name": "java"
}
},
"nbformat": 4,
"nbformat_minor": 0
}